/*
		ORIGINAL CODED BY WEBFX.EAE.NET
		ALL RIGHTS RESERVED.
		
		MODIFIED BY CASMEIRON
		PIMPAS FRAMEWORK
*/
// check browsers
var ua = navigator.userAgent;
var opera = /opera [56789]|opera\/[56789]/i.test(ua);
var ie = !opera && /MSIE/.test(ua);
var ie50 = ie && /MSIE 5\.[01234]/.test(ua);
var ie6 = ie && /MSIE [6789]/.test(ua);
var ieBox = ie && (document.compatMode == null || document.compatMode != "CSS1Compat");
var moz = !opera && /gecko/i.test(ua);
var nn6 = !opera && /netscape.*6\./i.test(ua);
// define the default values
simpleMenuDefaultWidth			= 100;

simpleMenuDefaultBorderLeft		= 1;
simpleMenuDefaultBorderRight	= 1;
simpleMenuDefaultBorderTop		= 1;
simpleMenuDefaultBorderBottom	= 1;
simpleMenuDefaultPaddingLeft	= 1;
simpleMenuDefaultPaddingRight	= 1;
simpleMenuDefaultPaddingTop		= 1;
simpleMenuDefaultPaddingBottom	= 1;
simpleMenuDefaultShadowLeft		= 0;
simpleMenuDefaultShadowRight	= ie && !ie50 && /win32/i.test(navigator.platform) ? 4 :0;
simpleMenuDefaultShadowTop		= 0;
simpleMenuDefaultShadowBottom	= ie && !ie50 && /win32/i.test(navigator.platform) ? 4 : 0;
simpleMenuItemDefaultHeight		= 18;
simpleMenuItemDefaultText		= "Untitled";
simpleMenuItemDefaultHref		= "javascript:void(0)";

simpleMenuSeparatorDefaultHeight	= 6;

simpleMenuDefaultEmptyText			= "Empty";
simpleMenuDefaultUseAutoPosition	= nn6 ? false : true;

// other global constants
simpleMenuImagePath				= "";

simpleMenuUseHover				= opera ? true : false;
simpleMenuHideTime				= 500;
simpleMenuShowTime				= 200;

var simpleMenuHandler = {
	idCounter		:	0,
	idPrefix		:	"simplemenu-menu-object-",
	all				:	{},
	getId			:	function () { return this.idPrefix + this.idCounter++; },
	handleAction    :   function(oItem) {
		var jsItem = this.all[oItem.id];
		if(jsItem.href == null || jsItem.href == simpleMenuItemDefaultHref) return;
		var bar = this.getBar(jsItem);
		bar._hiddenElement.value = jsItem.href;
		// get parent form
		var frm = pimpas.DOM.getParentForm(oItem);
		frm.submit( );
	},
	
	getBar			: function(jsItem) {
		if(jsItem == null) return;
		while(jsItem != null && !(jsItem instanceof simpleMenuBar))
			jsItem = jsItem.parentMenu;
		return jsItem;
	},
	
	overMenuItem	:	function (oItem) {
		if (this.showTimeout != null)
			window.clearTimeout(this.showTimeout);
		if (this.hideTimeout != null)
			window.clearTimeout(this.hideTimeout);
		var jsItem = this.all[oItem.id];
		if (simpleMenuShowTime <= 0)
			this._over(jsItem);
		else
			//this.showTimeout = window.setTimeout(function () { simpleMenuHandler._over(jsItem) ; }, simpleMenuShowTime);
			// I hate IE5.0 because the piece of shit crashes when using setTimeout with a function object
			this.showTimeout = window.setTimeout("simpleMenuHandler._over(simpleMenuHandler.all['" + jsItem.id + "'])", simpleMenuShowTime);
	},

	outMenuItem	:	function (oItem) {
		if (this.showTimeout != null)
			window.clearTimeout(this.showTimeout);
		if (this.hideTimeout != null)
			window.clearTimeout(this.hideTimeout);
		var jsItem = this.all[oItem.id];
		if (simpleMenuHideTime <= 0)
			this._out(jsItem);
		else
			//this.hideTimeout = window.setTimeout(function () { simpleMenuHandler._out(jsItem) ; }, simpleMenuHideTime);
			this.hideTimeout = window.setTimeout("simpleMenuHandler._out(simpleMenuHandler.all['" + jsItem.id + "'])", simpleMenuHideTime);
	},
	blurMenu		:	function (oMenuItem) {
		window.setTimeout("simpleMenuHandler.all[\"" + oMenuItem.id + "\"].subMenu.hide();", simpleMenuHideTime);
	},

	_over	:	function (jsItem) {
		if (jsItem.subMenu) {
			jsItem.parentMenu.hideAllSubs();
			jsItem.subMenu.show();
		}
		else
			jsItem.parentMenu.hideAllSubs();
		},

		_out	:	function (jsItem) {
		// find top most menu
		var root = jsItem;
		var m;
		if (root instanceof simpleMenuButton)
			m = root.subMenu;
		else {
			m = jsItem.parentMenu;
			while (m.parentMenu != null && !(m.parentMenu instanceof simpleMenuBar))
				m = m.parentMenu;
			}
			if (m != null)	
				m.hide();	
		},

		hideMenu	:	function (menu) {
		if (this.showTimeout != null)
			window.clearTimeout(this.showTimeout);
		if (this.hideTimeout != null)
			window.clearTimeout(this.hideTimeout);

		this.hideTimeout = window.setTimeout("simpleMenuHandler.all['" + menu.id + "'].hide()", simpleMenuHideTime);
	},

	showMenu	:	function (menu, src, dir) {
		if (this.showTimeout != null)
			window.clearTimeout(this.showTimeout);
		if (this.hideTimeout != null)
			window.clearTimeout(this.hideTimeout);

		if (arguments.length < 3)
			dir = "vertical";
		
		menu.show(src, dir);
	}
};

function simpleMenu() {
	this._menuItems	= [];
	this._subMenus	= [];
	this.id			= simpleMenuHandler.getId();
	this.top		= 0;
	this.left		= 0;
	this.shown		= false;
	this.parentMenu	= null;
	simpleMenuHandler.all[this.id] = this;
}

simpleMenu.prototype.width				= simpleMenuDefaultWidth;
simpleMenu.prototype.emptyText			= simpleMenuDefaultEmptyText;
simpleMenu.prototype.useAutoPosition 	= simpleMenuDefaultUseAutoPosition;
simpleMenu.prototype.borderLeft			= simpleMenuDefaultBorderLeft;
simpleMenu.prototype.borderRight		= simpleMenuDefaultBorderRight;
simpleMenu.prototype.borderTop			= simpleMenuDefaultBorderTop;
simpleMenu.prototype.borderBottom		= simpleMenuDefaultBorderBottom;

simpleMenu.prototype.paddingLeft		= simpleMenuDefaultPaddingLeft;
simpleMenu.prototype.paddingRight		= simpleMenuDefaultPaddingRight;
simpleMenu.prototype.paddingTop			= simpleMenuDefaultPaddingTop;
simpleMenu.prototype.paddingBottom		= simpleMenuDefaultPaddingBottom;

simpleMenu.prototype.shadowLeft			= simpleMenuDefaultShadowLeft;
simpleMenu.prototype.shadowRight		= simpleMenuDefaultShadowRight;
simpleMenu.prototype.shadowTop			= simpleMenuDefaultShadowTop;
simpleMenu.prototype.shadowBottom		= simpleMenuDefaultShadowBottom;

simpleMenu.prototype.add = function (menuItem) {
	this._menuItems[this._menuItems.length] = menuItem;
	if (menuItem.subMenu) {
		this._subMenus[this._subMenus.length] = menuItem.subMenu;
		menuItem.subMenu.parentMenu = this;
	}
	
	menuItem.parentMenu = this;
};

simpleMenu.prototype.show = function (relObj, sDir) {
	if (this.useAutoPosition)
		this.position(relObj, sDir);
	
	var divElement = document.getElementById(this.id);
	divElement.style.left = opera ? this.left : this.left + "px";
	divElement.style.top = opera ? this.top : this.top + "px";
	divElement.style.visibility = "visible";
	this.shown = true;
	if (this.parentMenu)
		this.parentMenu.show();
};

simpleMenu.prototype.hide = function () {
	this.hideAllSubs();
	var divElement = document.getElementById(this.id);
	divElement.style.visibility = "hidden";
	this.shown = false;
};

simpleMenu.prototype.hideAllSubs = function () {
	for (var i = 0; i < this._subMenus.length; i++) {
		if (this._subMenus[i].shown)
			this._subMenus[i].hide();
	}
};

simpleMenu.prototype.toString = function () {
	var top = this.top + this.borderTop + this.paddingTop;
	var str = "<div id='" + this.id + "' class='simplemenu-menu' style='" + 
	"width:" + (!ieBox  ?
		this.width - this.borderLeft - this.paddingLeft - this.borderRight - this.paddingRight  : 
		this.width) + "px;" +
	(this.useAutoPosition ?
		"left:" + this.left + "px;" + "top:" + this.top + "px;" :
		"") +
	(ie50 ? "filter: none;" : "") +
	"'>";
	
	if (this._menuItems.length == 0) {
		str +=	"<span class='simplemenu-menu-empty'>" + this.emptyText + "</span>";
	}
	else {	
		// loop through all menuItems
		for (var i = 0; i < this._menuItems.length; i++) {
			var mi = this._menuItems[i];
			str += mi;
			if (!this.useAutoPosition) {
				if (mi.subMenu && !mi.subMenu.useAutoPosition)
					mi.subMenu.top = top - mi.subMenu.borderTop - mi.subMenu.paddingTop;
				top += mi.height;
			}
		}

	}
	
	str += "</div>";

	for (var i = 0; i < this._subMenus.length; i++) {
		this._subMenus[i].left = this.left + this.width - this._subMenus[i].borderLeft;
		str += this._subMenus[i];
	}
	
	return str;
};

// simpleMenu.prototype.position defined later
function simpleMenuItem(sText, sHref, sToolTip, oSubMenu) {
	this.text = sText || simpleMenuItemDefaultText;
	this.href = (sHref == null || sHref == "") ? simpleMenuItemDefaultHref : sHref;
	this.subMenu = oSubMenu;
	if (oSubMenu)
		oSubMenu.parentMenuItem = this;
	this.toolTip = sToolTip;
	this.id = simpleMenuHandler.getId();
	simpleMenuHandler.all[this.id] = this;
};

simpleMenuItem.prototype.height = simpleMenuItemDefaultHeight;
simpleMenuItem.prototype.toString = function () {
	return	"<a" +
			" id='" + this.id + "'" +
			" href=\"" + this.href + "\"" +
			(this.toolTip ? " title=\"" + this.toolTip + "\"" : "") +
			" onmouseover='simpleMenuHandler.overMenuItem(this)'" +
			(simpleMenuUseHover ? " onmouseout='simpleMenuHandler.outMenuItem(this)'" : "") +
			(this.subMenu ? " unselectable='on' tabindex='-1'" : "") +
			">" +
			(this.subMenu ? "<img class='arrow' src=\"" + simpleMenuImagePath + "arrow.right.png\">" : "") +
			this.text + 
			"</a>";
};


function simpleMenuSeparator() {
	this.id = simpleMenuHandler.getId();
	simpleMenuHandler.all[this.id] = this;
};

simpleMenuSeparator.prototype.height = simpleMenuSeparatorDefaultHeight;
simpleMenuSeparator.prototype.toString = function () {
	return	"<div" +
			" id='" + this.id + "'" +
			(simpleMenuUseHover ? 
			" onmouseover='simpleMenuHandler.overMenuItem(this)'" +
			" onmouseout='simpleMenuHandler.outMenuItem(this)'"
			:
			"") +
			"></div>"
};

function simpleMenuBar(hidden_id) {
	this._parentConstructor = simpleMenu;
	this._parentConstructor();
	this._hiddenElement = document.getElementById(hidden_id);
	
	this.getHiddenElement = function( ) {
		return this._hiddenElement;	
	}
};

simpleMenuBar.prototype = new simpleMenu;

simpleMenuBar.prototype.toString = function () {
	var str = "<div id='" + this.id + "' class='simplemenu-menu-bar'>";
	
	// loop through all menuButtons
	for (var i = 0; i < this._menuItems.length; i++)
		str += this._menuItems[i];
	
	str += "</div>";

	for (var i = 0; i < this._subMenus.length; i++)
		str += this._subMenus[i];
	
	return str;
};

function simpleMenuButton(sText, sHref, sToolTip, oSubMenu) {
	this._parentConstructor = simpleMenuItem;
	this._parentConstructor(sText, sHref, sToolTip, oSubMenu);
};

simpleMenuButton.prototype = new simpleMenuItem;

simpleMenuButton.prototype.toString = function () {
	return	"<a" +
			" id='" + this.id + "'" +
			" href='" + this.href + "'" +
			(this.toolTip ? " title='" + this.toolTip + "'" : "") +
			(simpleMenuUseHover ?
				(" onmouseover='simpleMenuHandler.overMenuItem(this)'" +
				" onmouseout='simpleMenuHandler.outMenuItem(this)'") :
				(
					" onfocus='simpleMenuHandler.overMenuItem(this)'" +
					(this.subMenu ?
						" onblur='simpleMenuHandler.blurMenu(this)'" :
						""
					)
				)) +
			">" +
			this.text + 
			(this.subMenu ? " <img class='arrow' src='" + simpleMenuImagePath + "arrow.down.png' align='absmiddle'>" : "") +				
			"</a>";
};


/* Position functions */

function getInnerLeft(el) {
	if (el == null) return 0;
	if (ieBox && el == document.body || !ieBox && el == document.documentElement) return 0;
	return getLeft(el) + getBorderLeft(el);
};

function getLeft(el) {
	if (el == null) return 0;
	return el.offsetLeft + getInnerLeft(el.offsetParent);
};

function getInnerTop(el) {
	if (el == null) return 0;
	if (ieBox && el == document.body || !ieBox && el == document.documentElement) return 0;
	return getTop(el) + getBorderTop(el);
};

function getTop(el) {
	if (el == null) return 0;
	return el.offsetTop + getInnerTop(el.offsetParent);
};

function getBorderLeft(el) {
	return ie ?
		el.clientLeft :
		parseInt(window.getComputedStyle(el, null).getPropertyValue("border-left-width"));
};

function getBorderTop(el) {
	return ie ?
		el.clientTop :
		parseInt(window.getComputedStyle(el, null).getPropertyValue("border-top-width"));
};

function opera_getLeft(el) {
	if (el == null) return 0;
	return el.offsetLeft + opera_getLeft(el.offsetParent);
};

function opera_getTop(el) {
	if (el == null) return 0;
	return el.offsetTop + opera_getTop(el.offsetParent);
};

function getOuterRect(el) {
	return {
		left:	(opera ? opera_getLeft(el) : getLeft(el)),
		top:	(opera ? opera_getTop(el) : getTop(el)),
		width:	el.offsetWidth,
		height:	el.offsetHeight
	};
};

// mozilla bug! scrollbars not included in innerWidth/height
function getDocumentRect(el) {
	return {
		left:	0,
		top:	0,
		width:	(ie ?
					(ieBox ? document.body.clientWidth : document.documentElement.clientWidth) :
					window.innerWidth
				),
		height:	(ie ?
					(ieBox ? document.body.clientHeight : document.documentElement.clientHeight) :
					window.innerHeight
				)
	};
};

function getScrollPos(el) {
	return {
		left:	(ie ?
					(ieBox ? document.body.scrollLeft : document.documentElement.scrollLeft) :
					window.pageXOffset
				),
		top:	(ie ?
					(ieBox ? document.body.scrollTop : document.documentElement.scrollTop) :
					window.pageYOffset
				)
	};
};

/* end position functions */

simpleMenu.prototype.position = function (relEl, sDir) {
	var dir = sDir;
	// find parent item rectangle, piRect
	var piRect;
	if (!relEl) {
		var pi = this.parentMenuItem;
		if (!this.parentMenuItem)
			return;
		
		relEl = document.getElementById(pi.id);
		if (dir == null)
			dir = pi instanceof simpleMenuButton ? "vertical" : "horizontal";
		
		piRect = getOuterRect(relEl);
	}
	else if (relEl.left != null && relEl.top != null && relEl.width != null && relEl.height != null) {	// got a rect
		piRect = relEl;
	}
	else
		piRect = getOuterRect(relEl);
	
	var menuEl = document.getElementById(this.id);
	var menuRect = getOuterRect(menuEl);
	var docRect = getDocumentRect();
	var scrollPos = getScrollPos();
	var pMenu = this.parentMenu;
	
	if (dir == "vertical") {
		if (piRect.left + menuRect.width - scrollPos.left <= docRect.width)
			this.left = piRect.left;
		else if (docRect.width >= menuRect.width)
			this.left = docRect.width + scrollPos.left - menuRect.width;
		else
			this.left = scrollPos.left;
			
		if (piRect.top + piRect.height + menuRect.height <= docRect.height + scrollPos.top)
			this.top = piRect.top + piRect.height;
		else if (piRect.top - menuRect.height >= scrollPos.top)
			this.top = piRect.top - menuRect.height;
		else if (docRect.height >= menuRect.height)
			this.top = docRect.height + scrollPos.top - menuRect.height;
		else
			this.top = scrollPos.top;
	}
	else {
		if (piRect.top + menuRect.height - this.borderTop - this.paddingTop <= docRect.height + scrollPos.top)
			this.top = piRect.top - this.borderTop - this.paddingTop;
		else if (piRect.top + piRect.height - menuRect.height + this.borderTop + this.paddingTop >= 0)
			this.top = piRect.top + piRect.height - menuRect.height + this.borderBottom + this.paddingBottom + this.shadowBottom;
		else if (docRect.height >= menuRect.height)
			this.top = docRect.height + scrollPos.top - menuRect.height;
		else
			this.top = scrollPos.top;

		var pMenuPaddingLeft = pMenu ? pMenu.paddingLeft : 0;
		var pMenuBorderLeft = pMenu ? pMenu.borderLeft : 0;
		var pMenuPaddingRight = pMenu ? pMenu.paddingRight : 0;
		var pMenuBorderRight = pMenu ? pMenu.borderRight : 0;
		
		if (piRect.left + piRect.width + menuRect.width + pMenuPaddingRight +
			pMenuBorderRight - this.borderLeft + this.shadowRight <= docRect.width + scrollPos.left)
			this.left = piRect.left + piRect.width + pMenuPaddingRight + pMenuBorderRight - this.borderLeft;
		else if (piRect.left - menuRect.width - pMenuPaddingLeft - pMenuBorderLeft + this.borderRight + this.shadowRight >= 0)
			this.left = piRect.left - menuRect.width - pMenuPaddingLeft - pMenuBorderLeft + this.borderRight + this.shadowRight;
		else if (docRect.width >= menuRect.width)
			this.left = docRect.width  + scrollPos.left - menuRect.width;
		else
			this.left = scrollPos.left;
	}
};

/*
	ICON MENU ITEM
*/
function simpleMenuItemIcon(sIcon, sHoverIcon, sText, sHref, sToolTip, oSubMenu) {
	this.parentClass = simpleMenuItem;
	this.parentClass(sText, sHref, sToolTip, oSubMenu);
	
	this.icon = sIcon;
	this.hoverIcon = sHoverIcon || sIcon;
	
	// preload images
	if(this.icon != null) {
		this._preloadIcon = new Image();
		this._preloadIcon.src = this.icon;
	}
	if(this.hoverIcon != null) {
		this._preloadHoverIcon = new Image();
		this._preloadHoverIcon.src = this.hoverIcon;
	}
};

simpleMenuItemIcon.prototype = new simpleMenuItem;

simpleMenuItemIcon.prototype.toString = function () {
	var sOverIcon = (this.hoverIcon!=null) ? "var jso = simpleMenuHandler.all[this.id];" +
					"jso.setIcon(jso.hoverIcon);" : "";
	var sOutIcon = (this.icon != null) ? "var jso = simpleMenuHandler.all[this.id];" +
					"jso.setIcon(jso.icon);" : "";
	var sIconHtml = (this.icon == null) ? "" : "<img id='" + this.id + "-icon' src='" + this.icon + "' align='absmiddle' class='icon'/>";
	return	"<a" +
			" id='" + this.id + "'" +
			" onClick='simpleMenuHandler.handleAction(this)'" +
			(this.toolTip ? " title='" + this.toolTip + "'" : "") +
			" onmouseover='" + sOverIcon + "simpleMenuHandler.overMenuItem(this)'" +
			(simpleMenuUseHover ? " onmouseout='" + sOutIcon + "simpleMenuHandler.outMenuItem(this)'" :
				" onmouseout='" + sOutIcon + "'") +
			(this.subMenu ? " unselectable='on' tabindex='-1'" : "") +
			">" +
			(this.subMenu ? "<img class='arrow' src='" + simpleMenuImagePath + "arrow.right.png'>" : "") +
			sIconHtml +
			this.text + 
			"</a>";
};

simpleMenuItemIcon.prototype.setIcon = function (sSrc) {
	var imgEl = document.getElementById(this.id + "-icon");
	imgEl.src = sSrc;
};